package org.example.salesystem.controller;

import org.example.salesystem.entity.ResultBody;
import org.example.salesystem.service.impl.LoginServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/Login")
@CrossOrigin(origins="*")//跨域
public class LoginController {
    @Autowired
    private LoginServiceimpl loginServiceimpl;

    @RequestMapping("/LoginUser")
    public ResultBody LoginUser(@RequestBody Map<String, Object> map) {
        //判断参数完整
        if (map.get("phone") == null || map.get("password") == null || map.get("role") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        //转换成字符串类型，方便比较
        String phonenumber = map.get("phone").toString();
        String password= map.get("password").toString();
        String state = map.get("role").toString();
        //判断用户是否存在
        //密码是否匹配
        switch (state) {
            case "2":
                return loginServiceimpl.checkPasswordU(phonenumber, password);
            case "3":
                return loginServiceimpl.checkPasswordA(phonenumber, password);
        }
        return new ResultBody(400, "未知身份", false);
    }
}
